package com.springboot.spring01ioc.bean;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

//InitializingBean接口内含：Bean初始化
//DisposableBean接口内含：销毁Bean

@Data
public class User implements InitializingBean, DisposableBean {
    private String username;
    private String password;

    private Dog dog;

    @Autowired
    public void setDog(Dog dog){
        System.out.println("User  自动注入属性值：" + dog);
        this.dog = dog;
    }

    public User() {
        System.out.println("User  构造器");
    }

    //开始Bean的生命周期


    @PostConstruct//构造器之后，afterPropertiesSet()之前
    public void post(){
        System.out.println("User  @PostConstruct被执行");
    }

    @PreDestroy//在DisposableBean的destory的之前
    public void predestory(){
        System.out.println("User  @PreDestory被执行");
    }



    public void initUser(){
        System.out.println("User  @Bean  初始化");
    }

    public void destoryUser(){
        System.out.println("User  @Bean  销毁");
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("User  InitializingBean接口中afterPropertiesSet方法触发");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("User  DisposableBean接口中destory方法触发");
    }
}
